<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="__STATIC__/template/lib/html5shiv.js"></script>
    <script type="text/javascript" src="__STATIC__/template/lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/style.css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="__STATIC__/template/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <!--/meta 作为公共模版分离出去-->
    <style>

    </style>

    <title>角色添加</title>
</head>
<body>
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-admin-role-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$role.name}" id="name" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">权限解释：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$role.explain}" placeholder="" id="explain" name="explain">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">网站角色：</label>
            <div class="formControls col-xs-8 col-sm-9">
                {volist name="result" id="v" key="k"}
                <dl class="permission-list">
                    <dt>
                        <label>
							<input type="checkbox" value="{$v[0]['item_id']}" name="" class="item_id" onclick="changeMenu(this)"  >
						{$key}
						</label>
                    </dt>

                    <dd>
                        <dl class="cl permission-list2">
                            {foreach name="$v" item="value"}
                            <div style="display: inline-block;width: 150px">
                                <dt>
                                    <label class="menu">
                                        <input type="checkbox" value="{$value.id}" data-item-id="{$value.item_id}" name="menu_id" class="menu_id">
                                        <span>{$value.name}</span>
                                    </label>
                                </dt>
                            </div>
                            {/foreach}
                        </dl>

                    </dd>

                </dl>
                {/volist}
            </div>
        </div>
        <!-- 所有权限菜单 -->
        {foreach name='menus' item='v1'}
        <span style="color: red;display: none" class="all_menu">{$v1.id}</span>
        {/foreach}
        <!---->
        {foreach name='now_menu' item='nv'}
            {foreach name='nv' item='v2'}
         <span style="color: blue;display: none" class="now_menu">{$v2}</span>
            {/foreach}
        {/foreach}
        <input type="hidden" value="{$role.id}" id="role_id"/>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" class="btn btn-success radius" id="role-edit" name="admin-role-save">
                    <i class="icon-ok"></i> 确定
                </button>
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="__STATIC__/template/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
    $('document').ready(function(){
        $('#role-edit').click(roleUpdate);

        //获取所有权限菜单menu_id;
        var arr = new Array();
        var menu_len = $(".menu_id").length;
        for(var k=0;k<menu_len;k++){
            arr.push($(".menu_id").eq(k).val());
        }
      //  console.log(arr);

        //获取修改前所有选中的menu_id;
        var now_len = $(".now_menu").length;
        for(var i=0;i<now_len;i++){
            var val = $(".now_menu").eq(i).text();
            //console.log(val);
            var index = $.inArray(val,arr);
            if(index >= 0){
                // console.log(1);
                $(".menu_id:checkbox[value="+val+"]").attr('checked', 'true');
                var item =  $(".menu_id:checkbox[value="+val+"]").attr('data-item-id');
                $(".item_id:checkbox[value="+item+"]").attr('checked', 'true');
            }
        }

        // 选中一个 菜单 取消一个菜单   start
        $(".menu_id").click(function () {
            var item =   $(this).attr('data-item-id');
            if ($(this).is(":checked")) {
                $(this).prop("checked", true);
                 $(".item_id:checkbox[value="+item+"]").prop('checked', 'true');
                //alert("选中");
            } else {
                $(this).prop("checked", false);
                var l =  $("input[data-item-id="+item+"]:checked").length;  //获取菜单选中长度
                //console.log(l);
                if(l==0){
                    $(".item_id:checkbox[value="+item+"]").prop("checked", false);
                }
                //alert("取消");
            }
        });
        //选中一个 菜单 取消一个菜单   end

    });
    /*菜单 - 选择*/
    function changeMenu(obj){
        //alert(1);
        if($(obj).attr('checked','checked')){
            //alert(1);
            $(obj).closest("dl").find("dd input:checkbox").prop("checked",$(obj).prop("checked"));
        }
    }
    Array.prototype.in_array = function (element) {

        for (var i = 0; i < this.length; i++) {

            if (this[i] == element) {

                return true;

            }

        } return false;

    }
    /*修改 - 保存*/
    function roleUpdate(){
       // alert(123);
        var role_id = $('#role_id').val();
        var name 	= $('#name').val();
        var explain = $('#explain').val();
        var menu_id = new Array();
        var item_id   = new Array();

        var item_len = $("input:checkbox[class=item_id]:checked").length; //获取item长度
       // console.log(item_len);
        var all = "";
        for(var i=0;i<item_len;i++){
            var xx = $("input:checkbox[class=item_id]:checked").eq(i);
            //console.log(xx);
            var item_ids = xx.val();
            var menu_len = xx.parents('dt').next('dd').find("input:checkbox[class=menu_id]:checked").length; //获取menu的长度
            var menu = xx.parents('dt').next('dd').find("input:checkbox[class=menu_id]:checked");
            var menu_ids = '';
            for(var k=0;k<menu_len;k++){
                menu_ids += menu.eq(k).val()+",";
            }
            all += item_ids+':'+menu_ids+';';

        }
       // console.log(all);
        var url		= "{:url('admin/Role/roleUpdate')}";
        var data	= { role_id:role_id,name:name,explain:explain,all:all };
         $.post(url,data,function(res){
        // console.log(res);
         if(res.ok){
         window.parent.location.reload();//刷新父页面
         //var index = parent.layer.getFrameIndex(window.name);
         parent.layer.close();//关闭当前页面
         }else{
         layer.msg(res.msg,{icon:5,time:1000})
         }

         })
    }


</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
